<?php

// +----------------------------------------------------------------------
// | Ht.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace app\common\controller;

use think\Controller;

set_time_limit(0);

/**
 * 工具类
 * Class Tools
 * @package app\common\controller
 * @author 
 * @date 2017/10/12
 */
class Tools extends Controller
{
    private $savePath = 'temp/';

    public function __construct()
    {
        parent::__construct();

        date_default_timezone_set("Asia/Shanghai");

        $this->savePath = input('savepath','temp').'/';

        error_reporting(E_ERROR | E_WARNING);

        header("Content-Type: text/html; charset=utf-8");

    }

    /**
     * 图片上传
     * @desc 适用于基本上所有图片上传
     *
     * @param string $fieldName file上传input域
     * @param string $savePath 图片保存目录
     * @param bool $yearMonthDir 是否启用年月目录格式 默认“true”
     * @return string
     */
    public function upFile($fieldName, $savePath = '', $yearMonthDir = true){
        $file = request()->file($fieldName);

        if ($savePath != '') {
            $this->savePath = $savePath;
        }
//dump($file);die;
        $result = $this->validate(
            ['file2' => $file],
            ['file2'=>'image','file2'=>'fileSize:40000000'],
            ['file2.image' => '上传文件必须为图片','file2.fileSize' => '上传文件过大']
        );

        if (true !== $result || empty($file)) {
            $state = "ERROR" . $result;
        }else{
            // 移动到框架应用根目录/public/uploads/ 目录下
            if ($yearMonthDir) {
                $this->savePath = $this->savePath.'/'.date('Ym').'/';
            }else {
                $this->savePath = $this->savePath.'/';
            }

            // 使用自定义的文件保存规则
            $info = $file->rule(function ($file) {

                return  set_filename();
            })->move('static/upload/'.$this->savePath);
        }

        if($info){
            $url = '/static/upload/'.$this->savePath.$info->getSaveName();
            $data = array(
                'state' => 'SUCCESS',
                'url' => $url,
                'title' => $info->getFilename(),
                'original' => $info->getFilename(),
                //'original' => $info->getSaveName(),
                'type' => '.' . $info->getExtension(),
                'size' => $info->getSize(),
                'width' => get_width('.'.$url),
                'height' => get_height('.'.$url),
            );
            //图片加水印
            /*if($this->savePath=='goods/'){
                $image = new \Think\Image();
                $water = tpCache('water');
                $imgresource = ".".$data['url'];
                $image->open($imgresource);
                if($water['is_mark']==1 && $image->width()>$water['mark_width'] && $image->height()>$water['mark_height']){
                    if($water['mark_type'] == 'text'){
                        $image->text($water['mark_txt'],'./hgzb.ttf',20,'#000000',9)->save($imgresource);
                    }else{
                        $image->water(".".$water['mark_img'],9,$water['mark_degree'])->save($imgresource);
                    }
                }
            }*/
        }else{
            if ($info) {
                $state = $state.$info->getError();
            }
            $data = array('state' => $state);
        }
        return json_encode($data);
    }

    /**
     * andy-发送邮件
     * @desc PHPMail
     * @parameter string to_email 接受者邮箱地址（必须）
     * @parameter string subject 邮件主题（必须）
     * @parameter string content 邮件内容（必须）
     * @parameter string to_name 接受者名称（可选）
     * @method POST
     */
    public function email($toemail, $toName, $subject, $content)
    {

        $result = send_mail($toemail, $toName, $subject, $content);
        if($result) {
            return callback(true, '邮件发送成功', null);
        }else {
            return callback(false, $result, null);

        }
    }
}
